package ms.pms.controller;

import ms.common.exception.BizException;
import ms.common.page.Page;
import ms.common.util.string.StringUtil;
import ms.pms.bean.BindPermissionParamBean;
import ms.pms.bean.CommonQueryParamBean;
import ms.pms.bean.RoleParamBean;
import ms.pms.config.ResponseCode;
import ms.pms.entity.Role;
import ms.pms.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Project Name: ms
 * File Name: RoleController
 * Package Name: ms.pms.controller
 * Date: 2018/6/21  16:37
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    
    @Autowired
    private RoleService roleService;

    /**
     * 分页查询
     * @param commonQueryParamBean
     * @return
     */
    @PostMapping("/query")
    public Page<Role> query(@RequestBody CommonQueryParamBean commonQueryParamBean) {
        if(StringUtil.isEmpty(commonQueryParamBean.getPageNo())) {
            commonQueryParamBean.setPageNo(1);
        }
        if(StringUtil.isEmpty(commonQueryParamBean.getPageSize())) {
            commonQueryParamBean.setPageSize(10);
        }
        if(StringUtil.isEmpty(commonQueryParamBean.getStatus())) {
            commonQueryParamBean.setStatus(0);
        }

        Page<Role> page = roleService.query(commonQueryParamBean);
        return page;
    }

    /**
     * 查询
     * @param commonQueryParamBean
     * @return
     */
    @PostMapping("/list")
    public List<Role> list(@RequestBody CommonQueryParamBean commonQueryParamBean) {
        if(StringUtil.isEmpty(commonQueryParamBean.getStatus())) {
            commonQueryParamBean.setStatus(0);
        }
        if(StringUtil.isEmpty(commonQueryParamBean.getOptId())) {
            commonQueryParamBean.setOptId(0L);
        }

        return roleService.list(commonQueryParamBean);
    }

    /**
     * 保存
     * @param roleParamBean
     * @return
     */
    @PostMapping("/save")
    public void save(@RequestBody RoleParamBean roleParamBean) {
        //参数校验
        checkParam(roleParamBean);

        roleService.save(roleParamBean);
    }



    /**
     * 启用
     * @param id
     * @return
     */
    @PostMapping("/use/{id}")
    public void use(@PathVariable("id") Long id) {
        roleService.use(id);
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @PostMapping("/cancel/{id}")
    public void cancel(@PathVariable("id") Long id) {
        roleService.cancel(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public void delete(@PathVariable("id") Long id) {
        roleService.delete(id);
    }
    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping("/detail/{id}")
    public Role detail(@PathVariable("id") Long id) {
        return roleService.detail(id);
    }


    /**
     * 分配权限
     * @param bindPermissionParamBean
     */
    @PostMapping("/bind")
    public void bind(@RequestBody BindPermissionParamBean bindPermissionParamBean) {
        if(StringUtil.isEmpty(bindPermissionParamBean.getRoleId())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "角色不能为空");
        }
        if(StringUtil.isEmpty(bindPermissionParamBean.getPermissionIds())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "权限不能为空");
        }
        roleService.bind(bindPermissionParamBean);
    }

    /**
     * 校验新增和修改参数
     * @param roleParamBean
     */
    private void checkParam(RoleParamBean roleParamBean) {
        if(StringUtil.isEmpty(roleParamBean.getName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "角色名称不能为空");
        }
        if(StringUtil.isEmpty(roleParamBean.getCode())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "角色code不能为空");
        }
        if(StringUtil.isEmpty(roleParamBean.getStatus())) {
            roleParamBean.setStatus(2);
        }

    }
}
